﻿using ApplicationLibrary.SqlConnector;
using Vintner.Masters.ModuleData;

namespace Vintner.Items.ModuleData
{
    #region DRItems

    public class DRItems : DataReaderUpdater
    {
        private SqlABParameter p1;
        private string item;

        public DRItems()
            : base(false)
        { }

        protected override void CreateConnection()
        {
            myTable = VI_Items.Name;

            QueryBuilder qB = new QueryBuilder();
            p1 = new SqlABParameter("@p50", VI_Items.Item);

            qB.AddSelect();
            qB.AddFrom(VI_Items.Name);
            qB.AddCompare(VI_Items.Item, p1);

            sqlCM = new SqlABCommand(qB.Query, sqlCN);
            sqlCM.Parameters.Add(p1);
            sqlDA = new SqlABDataAdapter(sqlCM);
        }

        public bool Find(string item)
        {
            this.item = item;
            return base.Find();
        }

        protected override void SetParameters()
        {
            p1.Value = item;
        }
    }

    #endregion

    #region DRItemsPrice

    public class DRItemsPrice : DataReaderUpdater
    {
        private SqlABParameter p1, p2;
        private string item, category;

        public DRItemsPrice()
            : base(false)
        { }

        protected override void CreateConnection()
        {
            myTable = VI_ItemsPrice.Name;

            QueryBuilder qB = new QueryBuilder();
            p1 = new SqlABParameter("@p50", VI_ItemsPrice.Item);
            p2 = new SqlABParameter("@p51", VI_ItemsPrice.Category);

            qB.AddSelect();
            qB.AddFrom(VI_ItemsPrice.Name);
            qB.AddCompare(VI_ItemsPrice.Item, p1);
            qB.AddCompare(VI_ItemsPrice.Category, p2);

            sqlCM = new SqlABCommand(qB.Query, sqlCN);
            sqlCM.Parameters.Add(p1);
            sqlCM.Parameters.Add(p2);
            sqlDA = new SqlABDataAdapter(sqlCM);
        }

        public bool Find(string item, string category)
        {
            this.item = item;
            this.category = category;
            return base.Find();
        }

        protected override void SetParameters()
        {
            p1.Value = item;
            p2.Value = category;
        }
    }

    #endregion

    #region RRItemsPrice

    public class RRItemsPrice : DataReaderUpdater
    {
        private SqlABParameter p1;
        private string item;

        public RRItemsPrice()
            : base(false)
        { }

        protected override void CreateConnection()
        {
            myTable = VI_ItemsPrice.Name;

            QueryBuilder qB = new QueryBuilder();
            p1 = new SqlABParameter("@p50", VI_ItemsPrice.Item);

            qB.AddSelect();
            qB.AddFrom(VI_ItemsPrice.Name);
            qB.AddCompare(VI_ItemsPrice.Item, p1);

            sqlCM = new SqlABCommand(qB.Query, sqlCN);
            sqlCM.Parameters.Add(p1);
            sqlDA = new SqlABDataAdapter(sqlCM);
        }

        public bool Find(string item)
        {
            this.item = item;
            return base.Find();
        }

        protected override void SetParameters()
        {
            p1.Value = item;
        }
    }

    #endregion

    #region RRItemPriceCategory

    public class RRItemCategory : DataReaderUpdater
    {
        private SqlABParameter p1,p2;
        private string item = string.Empty;

        public RRItemCategory()
            : base(false)
        { }

        protected override void CreateConnection()
        {
            myTable = VI_ItemsPrice.Name;

            QueryBuilder qB = new QueryBuilder(true);
            p1 = new SqlABParameter("@p50", VI_ItemsPrice.Item);
            p2 = new SqlABParameter("@p51", VI_ItemsPrice.Disabled);

            qB.AddSelect();
            qB.AddSelect(VI_Descriptions.Description);
            qB.AddFrom(VI_ItemsPrice.Name);
            qB.AddJoin(QueryBuilder.JoinType.INNER, VI_Descriptions.Code, VI_ItemsPrice.Category);
            qB.AddJoinAnd<DescriptionType>(VI_Descriptions.Type, DescriptionType.E_Category);
            qB.AddCompare(VI_ItemsPrice.Item, p1);
            qB.AddCompare(VI_ItemsPrice.Disabled, p2);
            qB.AddOrder(VI_ItemsPrice.Category);

            sqlCM = new SqlABCommand(qB.Query, sqlCN);
            sqlCM.Parameters.Add(p1);
            sqlCM.Parameters.Add(p2);
            sqlDA = new SqlABDataAdapter(sqlCM);
        }

        public bool Find(string item)
        {
            this.item = item;
            return base.Find();
        }

        protected override void SetParameters()
        {
            p1.Value = item;
            p2.Value = false;
        }
    }

    #endregion

    #region DRItemCustomerPrice

    public class DRItemCustomerPrice : DataReaderUpdater
    {
        private SqlABParameter p1, p2;
        private string customer = string.Empty, item = string.Empty;

        public DRItemCustomerPrice()
            : base(false)
        { }

        protected override void CreateConnection()
        {
            myTable = VI_ItemCustomerPrice.Name;

            QueryBuilder qB = new QueryBuilder(true);
            p1 = new SqlABParameter("@p50", VI_ItemCustomerPrice.Customer);
            p2 = new SqlABParameter("@p51", VI_ItemCustomerPrice.Item);

            qB.AddSelect();
            qB.AddFrom(VI_ItemCustomerPrice.Name);
            qB.AddCompare(VI_ItemCustomerPrice.Customer, p1);
            qB.AddCompare(VI_ItemCustomerPrice.Item, p2);

            sqlCM = new SqlABCommand(qB.Query, sqlCN);
            sqlCM.Parameters.Add(p1);
            sqlCM.Parameters.Add(p2);
            sqlDA = new SqlABDataAdapter(sqlCM);
        }

        public bool Find(string customer, string item)
        {
            this.customer = customer;
            this.item = item;
            return base.Find();
        }

        protected override void SetParameters()
        {
            p1.Value = customer;
            p2.Value = item;
        }
    }

    #endregion

    #region RRItemCustomerPrice

    public class RRItemCustomerPrice : DataReaderUpdater
    {
        private SqlABParameter p1;
        private string customer = string.Empty;

        public RRItemCustomerPrice()
            : base(false)
        { }

        protected override void CreateConnection()
        {
            myTable = VI_ItemCustomerPrice.Name;

            QueryBuilder qB = new QueryBuilder(true);
            p1 = new SqlABParameter("@p50", VI_ItemCustomerPrice.Customer);

            qB.AddSelect(VI_Items.Description);
            qB.AddSelect();
            qB.AddFrom(VI_ItemCustomerPrice.Name);
            qB.AddJoin(QueryBuilder.JoinType.INNER, VI_Items.Item, VI_ItemCustomerPrice.Item);
            qB.AddCompare(VI_ItemCustomerPrice.Customer, p1);
            qB.AddOrder(VI_ItemCustomerPrice.Item);

            sqlCM = new SqlABCommand(qB.Query, sqlCN);
            sqlCM.Parameters.Add(p1);
            sqlDA = new SqlABDataAdapter(sqlCM);
        }

        public bool Find(string customer)
        {
            this.customer = customer;
            return base.Find();
        }

        protected override void SetParameters()
        {
            p1.Value = customer;
        }
    }

    #endregion
}